cmake_minimum_required(VERSION 2.8)

PROJECT(backtory C)

set (CMAKE_MODULE_PATH ${backtory_SOURCE_DIR}/../CMake)

file(READ ${backtory_SOURCE_DIR}/src/include/backtory/version.h BACKTORY_VERSION_H_CONTENTS)
string(REGEX MATCH "BACKTORY_VERSION_FLAVOUR[ \t]+\"([A-Z]+)\"" BACKTORY_VERSION_FL ${BACKTORY_VERSION_H_CONTENTS})
string(REGEX MATCH "(CURRENT|STABLE|ALPHA|BETA|PRERELEASE|RELEASE)" BACKTORY_VERSION_FL ${BACKTORY_VERSION_FL})
string(REGEX MATCH "BACKTORY_VERSION_MAJOR[ \t]+([0-9]+)" BACKTORY_VERSION_MJ ${BACKTORY_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" BACKTORY_VERSION_MJ ${BACKTORY_VERSION_MJ})
string(REGEX MATCH "BACKTORY_VERSION_MINOR[ \t]+([0-9]+)" BACKTORY_VERSION_MI ${BACKTORY_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" BACKTORY_VERSION_MI ${BACKTORY_VERSION_MI})
string(REGEX MATCH "BACKTORY_VERSION_PATCH[ \t]+([0-9]+)" BACKTORY_VERSION_PT ${BACKTORY_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" BACKTORY_VERSION_PT ${BACKTORY_VERSION_PT})
string(REGEX MATCH "BACKTORY_VERSION_SUFFX[ \t]+([0-9]+)" BACKTORY_VERSION_SX ${BACKTORY_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" BACKTORY_VERSION_SX ${BACKTORY_VERSION_SX})

string(COMPARE EQUAL ${BACKTORY_VERSION_FL} "RELEASE" BACKTORY_RELEASE)
if(BACKTORY_RELEASE)
	set(BACKTORY_VERSION ${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI}.${BACKTORY_VERSION_PT})
	set(BACKTORY_LIBVERSION ${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI})
else(BACKTORY_RELEASE)
	string(COMPARE EQUAL ${BACKTORY_VERSION_FL} "PRERELEASE" BACKTORY_PRERELEASE)
	if(BACKTORY_PRERELEASE)
		set(BACKTORY_VERSION ${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI}.${BACKTORY_VERSION_PT}-rc${BACKTORY_VERSION_SX})
		set(BACKTORY_LIBVERSION ${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI}-rc${BACKTORY_VERSION_SX})
	else(BACKTORY_PRERELEASE)
		string(COMPARE EQUAL ${BACKTORY_VERSION_FL} "BETA" BACKTORY_BETA)
		if(BACKTORY_BETA)
			set(BACKTORY_VERSION ${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI}.${BACKTORY_VERSION_PT}-b${BACKTORY_VERSION_SX})
			set(BACKTORY_LIBVERSION ${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI}-b${BACKTORY_VERSION_SX})
		else(BACKTORY_BETA)
			string(COMPARE EQUAL ${BACKTORY_VERSION_FL} "ALPHA" BACKTORY_ALPHA)
			if(BACKTORY_ALPHA)
				set(BACKTORY_VERSION ${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI}.${BACKTORY_VERSION_PT}-a${BACKTORY_VERSION_SX})
				set(BACKTORY_LIBVERSION ${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI}-a${BACKTORY_VERSION_SX})
			else(BACKTORY_ALPHA)
				string(COMPARE EQUAL ${BACKTORY_VERSION_FL} "STABLE" BACKTORY_STABLE)
				if(BACKTORY_STABLE)
					set(BACKTORY_VERSION ${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI}-STABLE)
					set(BACKTORY_LIBVERSION ${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI}-STABLE)
				else(BACKTORY_STABLE)
					set(BACKTORY_VERSION ${BACKTORY_VERSION_MJ}.0-CURRENT)
					set(BACKTORY_LIBVERSION ${BACKTORY_VERSION_MJ}.0-CURRENT)
				endif(BACKTORY_STABLE)
			endif(BACKTORY_ALPHA)
		endif(BACKTORY_BETA)
	endif(BACKTORY_PRERELEASE)
endif(BACKTORY_RELEASE)

set(PACKAGE "backtory")
set(PACKAGE_NAME ${PACKAGE})
set(PACKAGE_VERSION ${BACKTORY_VERSION})

set(BACKTORY_LIBS "")
set(CMAKE_C_FLAGS "-std=c99 -pedantic -Wall -Wextra -pipe -O3 -fno-strict-aliasing")
set(CMAKE_REQUIRED_FLAGS ${CMAKE_ANSI_CFLAGS})
set(CMAKE_REQUIRED_DEFINITIONS -D_FILE_OFFSET_BITS=64)
set(CMAKE_REQUIRED_LIBRARIES ${BACKTORY_LIBS})

add_subdirectory(src)
add_subdirectory(doc)
